Skip to content

Avoid caching native V8 stack overflow exceptions - #630

Draft
duke-101 wants to merge 1 commit into
benjamn:mainfrom
duke-101:codex/retry-native-stack-overflow
Draft

duke-101 wants to merge 1 commit into
benjamn:mainfrom
duke-101:codex/retry-native-stack-overflow

Conversation

@duke-101

Copy link
Copy Markdown

Problem

Refs apollographql/apollo-client#13534. Runnable Apollo reproduction: https://gist.github.com/duke-101/aacfc61fed4faeaf065822b18f915f40.

A native stack overflow can occur because the current caller has almost exhausted its stack.
Once that caller unwinds, the same computation can succeed without any input/dependency changes.
Caching the failure as clean instead keeps replaying the original RangeError.

Change

  • Keep entries holding the observed V8 RangeError dirty before reporting clean to their parents.
  • Preserve the first thrown error; a later caller can retry, and a successful retry is cached normally.
  • Preserve dirty-child propagation so parents that caught the error also reevaluate their fallback.
  • Preserve ordinary errors and thrown values, including values whose message/prototype inspection throws.
  • Add four regression cases for native overflow retry, transitive parent fallback, ordinary throw identity/caching, and subscription cleanup.

The classification is intentionally limited to a same-realm RangeError with the exact V8 message
"Maximum call stack size exceeded". Other RangeErrors, other engines' messages, promise results,
and ordinary exceptions keep their current behavior. This does not automatically retry a computation
or repair an infinite recursion.

Verification

  • npm ci --ignore-scripts, then npm run build: passes both TypeScript targets and Rollup outputs.
  • Before the source change: 47 passing, 3 failing, 1 existing pending in each ESM/CJS suite.
  • After the source change: 50 passing, 1 existing pending in each ESM/CJS suite on Node 22.23.3.
  • Packed the built library with npm pack and installed that tarball into isolated Apollo consumers.
    The reproduction recovers in all eight combinations:
    Apollo 3.13.4 / 4.3.8 × Node 22.23.5 / 26.11.1 × development / production.
    The corresponding unmodified npm-package runs reproduce the retained identical RangeError in all eight.
  • The normal cached-result/error and dependency tests remain enabled. No dependencies, lockfile,
    public types, or build configuration change.

The upstream Mocha runner on Node 26.11.1 fails before test collection in the existing yargs loader
("require is not defined in ES module scope"), so I am not claiming the full unit suite passed
there. The packed-library Apollo reproductions were executed successfully on Node 26 independently
of that runner.

Review scope

Opening as a draft for feedback on treating this V8 resource failure differently from ordinary
memoized exceptions (original exception-caching design).
Consumers will still need a published optimism release and a dependency update after any merge;
the Apollo issue should remain open until the consumer path is addressed.

Prepared with AI assistance. The reproductions, red/green unit suites, and packed-package checks
above were actually executed. Production-mode reduced-stack checks are not evidence of incidence
in a deployed production environment.

A native stack overflow can disappear once its caller has unwound, while
its query/input dependencies remain unchanged. Keeping that exception as
a clean memoized value prevents a later shallow call from recovering.

Leave only the observed V8 RangeError dirty before notifying parents, so
parents that caught the failure also reevaluate their fallback results.
Preserve normal result caching, ordinary exception identity and caching,
and subscription cleanup. Guard classification of arbitrary thrown values.

Add regression tests for native overflow retry, transitive parent fallback,
ordinary throw values with throwing getters, and subscription cleanup.
The new tests fail on the original implementation. After the change,
both ESM and CJS suites pass 50 tests with one existing pending case.

The packed npm artifact also fixes the Apollo reproduction across 3.13.4
and 4.3.8, Node 22.23.5 and 26.11.1, and both NODE_ENV modes.

Refs apollographql/apollo-client#13534.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant